package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.ExpendItem;
import cn.wolfcode.crm.query.ExpendItemQuery;
import cn.wolfcode.crm.service.IExpendItemService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("expendItem")
public class ExpendItemController {

    @Autowired
    IExpendItemService expendItemService;

    @RequestMapping("list")
    public String view() {
        return "/expendItem/list";
    }

    @RequestMapping("query")
    @ResponseBody
    public PageResult query(ExpendItemQuery qo) {
        expendItemService.query(qo);
        return expendItemService.query(qo);
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(ExpendItem expendItem) {
        //Employee user = UserContext.getCurrentUser();

        Employee user = new Employee();
        user.setUsername("111111");
        user.setId(1L);
        try {
            if (expendItem.getId() != null) {
                expendItem.setSpendUser(user);
                expendItemService.updateByPrimaryKey(expendItem);
            } else {
                expendItem.setSpendUser(user);
                expendItemService.insert(expendItem);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult("保存失败!");
        }
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<ExpendItem> selectAll() {
        return expendItemService.selectAll();
    }

    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        JsonResult result = new JsonResult();
        try {
            if (id != null) {
                expendItemService.delete(id);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult("删除失败！");
        }
    }

    //导出
    @RequestMapping("exportFile")
    public void exportFile(HttpServletResponse response) throws Exception {
        //设置响应头
        response.setHeader("Content-Disposition", "attachment;filename=expendItem.xls");
        //创建excel文件
        Workbook wb = new HSSFWorkbook();
        //创建工作簿
        Sheet sheet = wb.createSheet("一月份");
        //查询明细数据
        List<ExpendItem> items = expendItemService.selectAll();
        //创建第一行
        Row row = sheet.createRow(0);
        //设置标题
        row.createCell(0).setCellValue("支出分类");
        row.createCell(1).setCellValue("支出金额");
        row.createCell(2).setCellValue("支出时间");
        row.createCell(3).setCellValue("支出人员");
        row.createCell(3).setCellValue("备注");
        for (int i = 0; i < items.size(); i++) {
            //从第二行开始创建行
            row = sheet.createRow(i + 1);
            ExpendItem item = items.get(i);
            //创建单元格
            row.createCell(0).setCellValue(item.getExpend().getName());
            row.createCell(1).setCellValue(item.getAmount().toString());
            row.createCell(2).setCellValue(item.getSpendTime());
            row.createCell(3).setCellValue(item.getRemark());

        }
        //输出到浏览器
        wb.write(response.getOutputStream());
    }


    //查总金额
    @RequestMapping("selectTotalAmount")
    @ResponseBody
    public BigDecimal selectTotalAmount() {
        return expendItemService.selectTotalAmount();
    }
}

